package cn.xscrum.vtsp.controller;

import java.util.Date;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.xscrum.common.annotation.Log;
import cn.xscrum.common.enums.BusinessType;
import cn.xscrum.vtsp.domain.VtspAdvice;
import cn.xscrum.vtsp.service.IVtspAdviceService;
import cn.xscrum.common.core.controller.BaseController;
import cn.xscrum.common.core.domain.AjaxResult;
import cn.xscrum.common.utils.poi.ExcelUtil;
import cn.xscrum.common.core.page.TableDataInfo;

/**
 * 活动咨询记录Controller
 * 
 * @author Zxing
 * @date 2021-10-01
 */
@Controller
@RequestMapping("/vtsp/advice")
public class VtspAdviceController extends BaseController
{
        //静态资源前缀
        private String prefix = "vtsp/advice";

        //Service层对象
        @Autowired
        private IVtspAdviceService vtspAdviceService;

        //  访问主界面的控制器方法，返回 epms/advice/advice 页面

        /**
         * 返回静态资源templates中的"/vtsp/advice/advice"视图
         */
        @RequiresPermissions("vtsp:advice:view")
        @GetMapping()
        public String advice() {
            return prefix + "/advice";
        }

        /**
         * 按一定条件(vtspAdvice中附带条件)查询活动咨询记录列表
         */
        @RequiresPermissions("vtsp:advice:list")
        @PostMapping("/list")
        @ResponseBody
        public TableDataInfo list(VtspAdvice vtspAdvice) {
            startPage();
            //  因为传来的结束时间是yyyy-MM-dd 00:00:00, 将结束时间往后挪至次日前一秒
            if (vtspAdvice.getEndDate() != null) {
                //  Date -> 时间戳
                long timeStamp = vtspAdvice.getEndDate().getTime();
                //  时间戳 + (一天的秒数 - 1) * 1000
                timeStamp += (24 * 60 * 60 - 1) * 1000;
                //  时间戳 -> Date
                vtspAdvice.setEndDate(new Date(timeStamp));
            }
            //  根据vtspAdvice对象的属性查询
            List<VtspAdvice> list = vtspAdviceService.selectVtspAdviceList(vtspAdvice);
            return getDataTable(list);
        }

        /**
         * 修改活动咨询记录
         */
        @GetMapping("/edit/{adviceId}")
        public String edit(@PathVariable("adviceId") Long adviceId, ModelMap mmap) {
            VtspAdvice vtspAdvice = vtspAdviceService.selectVtspAdviceById(adviceId);
            mmap.put("vtspAdvice", vtspAdvice);
            return prefix + "/edit";
        }

        /**
         * 修改保存活动咨询记录
         */
        @RequiresPermissions("vtsp:advice:edit")
        @Log(title = "活动咨询记录", businessType = BusinessType.UPDATE)
        @PostMapping("/edit")
        @ResponseBody
        public AjaxResult editSave(VtspAdvice vtspAdvice) {
            return toAjax(vtspAdviceService.updateVtspAdvice(vtspAdvice));
        }






        
        //未用到的方法-----------------------------------------------------------------------------------------------------=
        /**
         * 导出活动咨询记录列表
         */
        @RequiresPermissions("vtsp:advice:export")
        @Log(title = "活动咨询记录", businessType = BusinessType.EXPORT)
        @PostMapping("/export")
        @ResponseBody
        public AjaxResult export(VtspAdvice vtspAdvice) {
            List<VtspAdvice> list = vtspAdviceService.selectVtspAdviceList(vtspAdvice);
            ExcelUtil<VtspAdvice> util = new ExcelUtil<VtspAdvice>(VtspAdvice.class);
            return util.exportExcel(list, "advice");
        }

        /**
         * 新增活动咨询记录
         */
        @GetMapping("/add")
        public String add() {
            return prefix + "/add";
        }

        /**
         * 新增保存活动咨询记录
         */
        @RequiresPermissions("vtsp:advice:add")
        @Log(title = "活动咨询记录", businessType = BusinessType.INSERT)
        @PostMapping("/add")
        @ResponseBody
        public AjaxResult addSave(VtspAdvice vtspAdvice) {
            return toAjax(vtspAdviceService.insertVtspAdvice(vtspAdvice));
        }

        /**
         * 删除活动咨询记录
         */
        @RequiresPermissions("vtsp:advice:remove")
        @Log(title = "活动咨询记录", businessType = BusinessType.DELETE)
        @PostMapping("/remove")
        @ResponseBody
        public AjaxResult remove(String ids) {
            return toAjax(vtspAdviceService.deleteVtspAdviceByIds(ids));
        }
    }